package com.acquisition.controller;

import com.acquisition.common.entity.BaseResponse;
import com.acquisition.entity.SysUser;
import com.acquisition.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    SysUserService sysUserService;

    /**
     * @Author Jay Chou
     * @Description 通过id查询用户信息
     * @Date 2023-04-12 13:41
     * @param id
     * @return com.acquisition.common.entity.BaseResponse<com.acquisition.entity.SysUser>
     **/
    @GetMapping("/queryUserById")
    public BaseResponse<SysUser> queryUserById(String id) {
        SysUser sysUser = sysUserService.queryById(id);
        return BaseResponse.success(sysUser);
    }

    /**
     * @Author Jay Chou
     * @Description 按条件筛选分页查询用户列表
     * @Date 2023-04-12 13:42
     * @param page
     * @param sysUser
     * @return com.acquisition.common.entity.BaseResponse<com.github.pagehelper.PageInfo<com.acquisition.entity.SysUser>>
     **/
    @GetMapping("/queryUserList")
    public BaseResponse<PageInfo<SysUser>> queryUserList(PageInfo page, SysUser sysUser) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysUser> sysUsers = sysUserService.queryUserList(sysUser);
        return BaseResponse.success(new PageInfo<>(sysUsers));
    }

    /**
     * @Author Jay Chou
     * @Description 新增用户
     * @Date 2023-04-12 13:43
     * @param sysUser
     * @return com.acquisition.common.entity.BaseResponse<com.acquisition.entity.SysUser>
     **/
    @GetMapping("/insertUser")
    public BaseResponse<SysUser> insertUser(SysUser sysUser) {
        if(sysUserService.insert(sysUser)>0){
            return BaseResponse.success("新增用户成功");
        }
        return BaseResponse.fail("新增用户失败");
    }

    /**
     * @Author Jay Chou
     * @Description 修改用户
     * @Date 2023-04-12 13:43
     * @param sysUser
     * @return com.acquisition.common.entity.BaseResponse<com.acquisition.entity.SysUser>
     **/
    @GetMapping("/updateUser")
    public BaseResponse<SysUser> updateUser(SysUser sysUser) {
        if (sysUserService.update(sysUser) > 0) {
            return BaseResponse.success("修改用户成功");
        }
        return BaseResponse.fail("修改用户失败");
    }

    /**
     * @Author Jay Chou
     * @Description 删除用户
     * @Date 2023-04-12 13:44
     * @param id
     * @return com.acquisition.common.entity.BaseResponse<com.acquisition.entity.SysUser>
     **/
    @GetMapping("/deleteUser")
    public BaseResponse<SysUser> deleteUser(String id) {
        if (sysUserService.deleteById(id) > 0) {
            return BaseResponse.success("删除用户成功");
        }
        return BaseResponse.fail("删除用户失败");
    }
}
